package com.study.favorite.servlet;

import com.google.gson.Gson;
import com.study.favorite.Result;
import com.study.favorite.biz.FavBiz;
import com.study.favorite.biz.FavBizImpl;
import com.study.favorite.pojo.Favorite;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "AddFavServlet", value = "/addFav.s")
public class AddFavServlet extends HttpServlet {

    private FavBiz favBiz = new FavBizImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");

        String flable = request.getParameter("flable");
        String furl = request.getParameter("furl");
        String fdesc = request.getParameter("fdesc");
        String ftags = request.getParameter("ftags");

        Favorite favorite = new Favorite();
        favorite.setFlable(flable);
        favorite.setFurl(furl);
        favorite.setFdesc(fdesc);
        favorite.setFtags(ftags);

        favBiz.addFav(favorite);

        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");

        String json = new Gson().toJson(new Result(1, "添加成功", null));
        response.getWriter().append(json);

    }
}
